﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using TomShane.Neoforce.Controls;

namespace BlackAndWhiteBalance
{
    public class Control
    {
        Core core;

        int id;
        public int Id
        {
            get { return id; }
            set { id = value; }
        }

        private Vector2 position;

        public Vector2 Position
        {
            get { return position; }
            set { position = value; UpdatePosition(); }
        }
        public float PositionX
        {
            get { return position.X; }
            set { position.X = value; }
        }
        public float PositionY
        {
            get { return position.Y; }
            set { position.Y = value; }
        }

        public enum ControlType { Repulsive, Attractive, PushDown, PushUp }
        public float strength = 1f;

        private ControlType type;
        public ControlType Type
        {
            get { return type; }
            set 
            {
                type = value;
                SetIcon();
            }
        }

        bool enabled = true;
        public bool Enabled
        {
            get { return enabled; }
            set
            {
                enabled = value;
                SetIcon();
            }
        }

        bool isSelected = true;
        public bool IsSelected
        {
            get { return isSelected; }
            set
            {
                isSelected = value;
                SetIcon();
            }
        }

        private void SetIcon()
        {
            if (visible)
            {
                if (enabled)
                {
                    if (isSelected)
                    {
                        switch (type)
                        {
                            case ControlType.Attractive:
                                icon.Image = AttractiveIconEnabledSelected;
                                break;
                            case ControlType.Repulsive:
                                icon.Image = RepulsiveIconEnabledSelected;
                                break;
                            case ControlType.PushDown:
                                icon.Image = PushDownIconEnabledSelected;
                                break;
                            case ControlType.PushUp:
                                icon.Image = PushUpIconEnabledSelected;
                                break;
                        }
                    }
                    else
                    {
                        switch (type)
                        {
                            case ControlType.Attractive:
                                icon.Image = AttractiveIconEnabled;
                                break;
                            case ControlType.Repulsive:
                                icon.Image = RepulsiveIconEnabled;
                                break;
                            case ControlType.PushDown:
                                icon.Image = PushDownIconEnabled;
                                break;
                            case ControlType.PushUp:
                                icon.Image = PushUpIconEnabled;
                                break;
                        }
                    }
                }
                else
                {
                    if (isSelected)
                    {
                        switch (type)
                        {
                            case ControlType.Attractive:
                                icon.Image = AttractiveIconDisabledSelected;
                                break;
                            case ControlType.Repulsive:
                                icon.Image = RepulsiveIconDisabledSelected;
                                break;
                            case ControlType.PushDown:
                                icon.Image = PushDownIconDisabledSelected;
                                break;
                            case ControlType.PushUp:
                                icon.Image = PushUpIconDisabledSelected;
                                break;
                        }
                    }
                    else
                    {
                        switch (type)
                        {
                            case ControlType.Attractive:
                                icon.Image = AttractiveIconDisabled;
                                break;
                            case ControlType.Repulsive:
                                icon.Image = RepulsiveIconDisabled;
                                break;
                            case ControlType.PushDown:
                                icon.Image = PushDownIconDisabled;
                                break;
                            case ControlType.PushUp:
                                icon.Image = PushUpIconDisabled;
                                break;
                        }
                    }
                }
            }
            else
            {
                icon.Image = InvisbleIcon;
            }
        }

        bool visible = false;
        public bool Visible
        {
            get { return visible; }
            set
            {
                visible = value;
                SetIcon();
            }
        }

        ImageBox icon;

        public static Texture2D AttractiveIconEnabled;
        public static Texture2D RepulsiveIconEnabled;
        public static Texture2D PushUpIconEnabled;
        public static Texture2D PushDownIconEnabled;

        public static Texture2D AttractiveIconDisabled;
        public static Texture2D RepulsiveIconDisabled;
        public static Texture2D PushUpIconDisabled;
        public static Texture2D PushDownIconDisabled;

        public static Texture2D AttractiveIconEnabledSelected;
        public static Texture2D RepulsiveIconEnabledSelected;
        public static Texture2D PushUpIconEnabledSelected;
        public static Texture2D PushDownIconEnabledSelected;

        public static Texture2D AttractiveIconDisabledSelected;
        public static Texture2D RepulsiveIconDisabledSelected;
        public static Texture2D PushUpIconDisabledSelected;
        public static Texture2D PushDownIconDisabledSelected;

        public static Texture2D InvisbleIcon;

        public Control(Core core, Manager manager, int id)
        {
            this.core = core;

            icon = new ImageBox(manager);
            icon.Init();
            icon.Width = 26;
            icon.Height = 26;
            icon.Top = 0;
            icon.Left = 0;
            icon.Click += new TomShane.Neoforce.Controls.EventHandler(icon_Click);

            manager.Add(icon);

            ResetToNonExistant(id);
        }

        public void ResetToNonExistant(int id)
        {
            this.id = id;

            Type = ControlType.Attractive;
            Visible = false;
            Enabled = true;
            Position = Vector2.Zero;
        }

        void icon_Click(object sender, TomShane.Neoforce.Controls.EventArgs e)
        {
            core.ClickControl(id);
        }

        private void UpdatePosition()
        {
            icon.Left = ((int)position.X * Const.PlayAreaToScreenScale) + Const.PlayAreaRenderRectangle.Left - (icon.Width / 2);
            icon.Top = ((int)position.Y * Const.PlayAreaToScreenScale) + Const.PlayAreaRenderRectangle.Top - (icon.Height / 2);
        }
    }
}
